package com.starmark.gateway.auth.provider.service.impl;


import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.starmark.gateway.auth.api.service.IAuthApiService;
import com.starmark.gateway.auth.provider.service.IGatewayAuthResourceService;
import com.starmark.gateway.auth.provider.service.IGatewayAuthRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 权限服务接口
 *
 * @author starmark
 * @since  2018-05-01 21:32
 **/
@Service("gatewayAuthApiService")
public class GatewayAuthApiServiceImpl implements IAuthApiService {

    /**
     * 角色服务
     */
    @Autowired
    private IGatewayAuthRoleService gatewayAuthRoleService;


    /**
     * 权限资源服务
     */
    @Autowired
    private IGatewayAuthResourceService gatewayAuthResourceService;

    @Cached(name = "gatewayAuthApiService:getRoleIdsByOrgId:", key = "#orgIds+'_'+#projectCode",cacheType=CacheType.BOTH)
    @Override
    public Set<String> getRoleIdsByOrgId(List<String> orgIds,String projectCode) {

        return gatewayAuthRoleService.getRoleIdsByOrgId(orgIds,projectCode);
    }

    @Override
    @Cached(name = "gatewayAuthApiService:isAdmin:", key = "#orgId+'_'+#projectCode",cacheType=CacheType.BOTH)
    public boolean isAdmin(String orgId, String projectCode) {
        return gatewayAuthRoleService.isAdmin(orgId,projectCode);
    }



    /**
     * 通过组织ID,查找相关权限资源ID
     *
     * @param orgIds 组经ID
     * @return 资源ID
     */
    @Cached(name = "gatewayAuthResourceService:getPermissionIdsByOrgId:", key = "#orgIds",cacheType=CacheType.BOTH)
    @Override
    public Set<String> getPermissionIdsByOrgId(List<String> orgIds) {
        return gatewayAuthResourceService.getPermissionIdsByOrgId(orgIds);
    }

    @Override
    @Cached(name = "sysAuthApiService:getPermissionByUrl:", key = "#checkUrl",cacheType=CacheType.BOTH)
    public Set<String> getPermissionByUrl(String checkUrl, String methodType) {
        return gatewayAuthResourceService.getPermissionByUrl(checkUrl, methodType);
    }

}
